#设置各种参数
#typeset MIP='127.0.0.1'
#typeset dbmpassword='123456'
# typeset SIP='10.27.3.176'               #从服务器ip
# typeset hmuser='root'                   #主服务器远程用户
# typeset hmpassword='1qaz@WSX'           #主服务器远程密码
# typeset hsuser='root'                   #从服务器远程用户
# typeset hspassword='1qaz@WSX'           #从服务器远程密码
# typeset dbsuser='root'                  #从服务器mysql用户
# typeset dbspassword='QWEasdzxc#123'     #从服务器mysql密码
# typeset sconfpath='/etc/my.cnf'         #从mysql配置文件路径

typeset MIP='10.27.3.175'               #主服务器ip
typeset dbmuser='root'                  #主服务器mysql用户
typeset dbmpassword='QWEasdzxc#123'     #主服务器mysql密码
typeset check_times=''                  #命令重复次数
typeset mconfpath='/etc/my.cnf'         #主mysql配置文件路径
#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer';exit 1
fi

#配置日志路径
typeset SCRIPT_NAME="mysql_master_$MIP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f "${LOG_FILE}" ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

log_format "begin to build mysql master................................."

#检查配置文件是否存在
if [ ! "$mconfpath" ];then
    cnfnum=`find / -name my.cnf|wc -l`
    if [ "$cnfnum" -gt 1 ];then
        log_format 'ERROR' 'number of my.cnf is more than one,please input my.cnf path' && exit 1
    else
        mconfpath=`find / -name my.cnf `
        log_format 'INFO' "find my.cnf path: $mconfpath"
    fi
else
    if [ ! -f "$mconfpath" ];then
        log_format 'ERROR' "my.cnf does not exit,check my.cnf path" && exit 1
    else
        log_format 'INFO' "my.cnf exits"
    fi
fi



#备份配置文件
time=`date +%Y%m%d`
backpath=${mconfpath}_${time}.bak
if [ ! -f "$backpath" ];then
   cp $mconfpath $backpath
    if [ ! -f "$backpath" ];then
        log_format 'ERROR' "copy my.cnf failed" && exit 1
    else
        log_format 'INFO' "copy my.cnf success"
    fi
else
    log_format 'INFO' "my.cnf copy exits"
fi

#检查mysql命令路径
basedir=`cat $mconfpath|grep -v ^#|grep basedir|awk -F'=' '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
if [ ! "$basedir" ];then
    log_format 'ERROR' "can not find basedir" && exit 2
else  
    mycmdpath=$basedir/bin/mysql
    if [ ! -e "$mycmdpath" ];then
        log_format 'ERROR' "can not find mysql cmd $mycmdpath" && exit 2
    else
        log_format 'INFO' "mysql cmd finded $mycmdpath"
    fi
fi

#检查主配置文件配置
##检查或修改slave-skip-errors=all
cat $mconfpath|grep -v ^#|grep slave-skip-errors > /dev/null
if [ $? -ne 0 ];then
    sed  -i '/\[mysqld\]/a slave-skip-errors=all' $mconfpath
    log_format 'INFO' "slave-skip-errors added"
else
    sed -i 's/slave-skip-erros.*/slave-skip-errors=all/' $mconfpath
    log_format 'INFO' "slave-skip-error changed"
fi

##检查或修改log-bin=mysql-bin-master
cat $mconfpath|grep -v ^#|grep log-bin > /dev/null
if [ $? -ne 0 ];then
    sed  -i '/\[mysqld\]/a log-bin=mysql-bin-master' $mconfpath
    log_format 'INFO' "log-bin added"
else
    sed -i 's/log-bin.*/log-bin=mysql-bin-master/' $mconfpath
    log_format 'INFO' "log-bin changed"
fi

##添加或修改server-id
ID=`echo $MIP|awk -F'.' '{print 1$3$4}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
cat $mconfpath|grep -v ^#|grep server-id > /dev/null
if [ $? -ne 0 ];then
    sed  -i "/\[mysqld\]/a server-id=$ID" $mconfpath
    log_format 'INFO' "server-id added"
else
    sed -i "s/server-id.*/server-id=$ID/" $mconfpath
    log_format 'INFO' "server-id changed"
fi

##删除gtid_mode配置
gtidnum1=`cat $mconfpath|grep -v ^#|grep 'gtid_mode'|wc -l`
gtidnum2=`cat $mconfpath|grep -v ^#|grep 'gtid-mode'|wc -l`
if [ $gtidnum1 -eq 0 ] && [ $gtidnum2 -eq 0 ];then
    log_format 'INFO' 'no gtid information in my.cnf'
else
    sed -i "/gtid_mode/d" $mconfpath
    sed -i "/gtid-mode/d" $mconfpath
    gtidnum1=`cat $mconfpath|grep -v ^#|grep 'gtid_mode'|wc -l`
    gtidnum2=`cat $mconfpath|grep -v ^#|grep 'gtid-mode'|wc -l`
    if [ $gtidnum1 -eq 0 ] && [ $gtidnum2 -eq 0 ];then
        log_format 'INFO' 'gtid information has been deleted in my.cnf'
    else
        log_format 'ERROR' 'failed to delete gtid information in my.cnf' && exit 1
    fi
fi

##删除enforce_gtid_consistenc配置
enforcenum1=`cat $mconfpath|grep -v ^#|grep 'enforce_gtid_consistency'|wc -l`
enforcenum2=`cat $mconfpath|grep -v ^#|grep 'enforce-gtid-consistency'|wc -l`
if [ $enforcenum1 -eq 0 ] && [ $enforcenum2 -eq 0 ];then
    log_format 'INFO' 'no enforce information in my.cnf'
else
    sed -i "/enforce_gtid_consistency/d" $mconfpath
    sed -i "/enforce-gtid-consistency/d" $mconfpath
    enforcenum1=`cat $mconfpath|grep -v ^#|grep 'enforce_gtid_consistency'|wc -l`
    enforcenum2=`cat $mconfpath|grep -v ^#|grep 'enforce-gtid-consistency'|wc -l`
    if [ $enforcenum1 -eq 0 ] && [ $enforcenum2 -eq 0 ];then
        log_format 'INFO' 'enforce information has been deleted in my.cnf'
    else
        log_format 'ERROR' 'failed to delete enforce information in my.cnf' && exit 1
    fi
fi

#重启mysql
log_format 'INFO' "begin to restart mysql"
systemctl restart mysql || (log_format 'ERROR' "Failed to restart mysqld.service: Unit not found." && exit 1)
reok=false
for ((i=0;i<${check_times};i++))
do
    sleep 3
    mysqlnum=`ps -ef|grep -w 'bin/mysqld'|grep -v grep|wc -l`
    if [ $mysqlnum -eq 0 ];then
        log_format 'WARNING' "wait mysql to start "      
    else
        log_format 'INFO' "mysql restart success"
        reok=true
        break
    fi
done
if [ $reok == false ];then
    log_format 'ERROR' "start mysql failed" && exit 1
fi

#添加repl用户
log_format 'INFO' "begin to add repl user"
mycmd="$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword"
checkuser=`$mycmd   -e "select * from mysql.user where User='repl'\G"`
echo $checkuser
if [ ! "$checkuser" ];then
    log_format "WARNING" "there is no reple user,create a reple user"
    $mycmd  <<eof
use mysql;
create user 'repl'@'%' identified by 'Hatech1618';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
eof
    if [ $? -ne 0 ];then
        log_format "ERROR" "create reple user failed" && exit 1
    else
        log_format "INFO" "reple user create success"
    fi
else
    log_format "WARNING" "warning repl user exits"
fi




#查看mysql主服务器状态
log_format 'INFO' "begin to  check master status"
statusnum=`$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword -e "show master status;"|grep bin |wc -l`
if [ $statusnum -eq 0 ];then
    log_format 'ERROR' "mysql master status is nil!" && exit 1
else
    status=`$mycmdpath -h$MIP -u$dbmuser -p$dbmpassword -e "show master status;"|grep bin`
    echo $status
    binlog=`echo $status|awk '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    position=`echo $status|awk '{print $2}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    if [ ! "$binlog" ] || [ ! $position ];then
        log_format 'ERROR' "fail to get binlogname or position" && exit 1
    else
        expr 1 + ${position} &>/dev/null
        if [ $? -ne 0 ];then
            log_format "ERROR" 'get wrong position value , not integer' && exit 1
        fi
        log_format "INFO" 'binlogname and postion value get success!'
    fi
fi

echo "binlog: $binlog"
echo "position: $position"
